package cn.edu.sjtu.se.dslab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.project.DisplayZone;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IDomainEntityService;
import cn.edu.sjtu.se.dslab.service.IDisplayZoneService;
import cn.edu.sjtu.se.dslab.service.IProjectService;

@Controller
@RequestMapping(value="/displayZone")
public class DisplayZoneController {

	private static final Logger logger = Logger.getLogger(DisplayZoneController.class);

	@Resource(name = "domainEntityService")
	IDomainEntityService domainEntityService;
	
	@Resource(name = "displayZoneService")
	IDisplayZoneService displayZoneService;
	
	@Resource(name = "projectService")
	IProjectService projectService;
	
	
	@RequestMapping(value = "/{projectId}/list", method = RequestMethod.GET)
	public ModelAndView getDisplayZoneByProject(@PathVariable(value = "projectId")long projectId){
		ModelAndView mav = new ModelAndView();
		List<DisplayZone> displayZones = displayZoneService.getDisplayZoneByProject(projectId);
		mav.addObject("displayZones", displayZones);
		for (DisplayZone displayZone : displayZones){
			DomainEntity entity = domainEntityService.getDomainEntityByUri(displayZone.getEntityUri());
			if (entity!=null){
				displayZone.setEntityUri(entity.getName());
			}
		}
		
		List<Project> projects = projectService.getProjectList();
		mav.addObject("projects", projects.iterator());
		
		Project project = projectService.getProjectById(projectId);
		mav.addObject("project", project);
		
		List<DomainEntity> domainEntities = domainEntityService
				.getAllDomainEntity();
		mav.addObject("domainEntitys", domainEntities);
		
		mav.setViewName("editDisplayZone");
		return mav;
	}
	
	
	@RequestMapping(value = "/{type}/{projectId}/add", method = RequestMethod.POST)
	public String addDisplayZone(@PathVariable(value = "projectId")long projectId,
			@PathVariable(value = "type")String type, DisplayZone displayZone){
		ModelAndView mav = new ModelAndView();
		List<DisplayZone> displayZones = displayZoneService.getDisplayZoneByProject(projectId);
		mav.addObject("displayZones", displayZones);
		displayZoneService.addDisplayZone(displayZone, projectId);
		StringBuilder sb = new StringBuilder("redirect:/");
		sb.append(type).append("/").append(projectId).append("/design_pattern_component/DISPLAY/edit");
		return sb.toString();
	}
	
	@RequestMapping(value = "/{projectId}/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addDisplayZone(@PathVariable(value = "projectId")long projectId,@RequestBody DisplayZone displayZone){
		Map<String, String> responseMap = new HashMap<String, String>();
		logger.info("Add DisplayZone name=" + displayZone.getName() + " size:" + displayZone.getSize() + " entityUri:" + displayZone.getEntityUri());
		boolean result = displayZoneService.addDisplayZone(displayZone, projectId);
		if (result){
			responseMap.put("result", Boolean.TRUE.toString());
		}else{
			responseMap.put("result", Boolean.FALSE.toString());
		}
		return responseMap;
	}
	
	
	@RequestMapping(value = "/{projectId}/{displayZoneId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, String> deleteDisplayZone(@PathVariable(value = "projectId")long projectId, 
			@PathVariable(value = "displayZoneId")long displayZoneId){
		logger.info("Delete entity zone" + displayZoneId);
		Map<String, String> responseMap = new HashMap<String, String>();
		try {
			boolean result = displayZoneService.deleteDisplayZoneById(displayZoneId);
			if (result) {
				responseMap.put("result", Boolean.TRUE.toString());
			} else {
				responseMap.put("result", Boolean.FALSE.toString());
			}
		} catch (Exception ex) {
			responseMap.put("result", Boolean.FALSE.toString());
		}
		return responseMap;
	}
	

	@ExceptionHandler({ IOException.class, DBException.class })
	public ResponseEntity<String> handleIOException(IOException ex) {
		// prepare responseEntity
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.TEXT_PLAIN);
		ResponseEntity<String> responseEntity = new ResponseEntity<String>(
				"The server has some errors,please try later!", headers,
				HttpStatus.OK);
		return responseEntity;
	}
	
	
	
	
	
	
}
